
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        loadStatus: false,
        detail: {},
        schoolIds: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        // options.id = 1;

        console.log(options)

        this.setData({
            id: options.id
        })

        wx.showLoading();
        this.get_detail(() => {
            this.setData({
                loadStatus: true
            })
            wx.hideLoading();
        })
    },
    //获取详情信息
    get_detail(callback) {
        let that = this;
        app.request({
            url: 'enrollment/detail',
            data: {
                id: that.data.id
            },
            sCallback: (res) => {
                if (res.code == 1) {
                    that.setData({
                        detail: res.data,
                    })


                    callback && callback();
                } else {
                    app.alert(res.msg, () => {
                        pages = getCurrentPages();
                        if (pages.length > 1) {
                            wx.navigateBack();
                        } else {
                            wx.navigateTo({
                                url: '/pages/index/index'
                            })
                        }
                    })
                }
            }
        })
    },
    confirm() {
        let that = this;
        let ids = this.data.schoolIds;
        if (!ids) {
            app.alert('请选择你要上课的机构');
            return false;
        }

        wx.showModal({
            title: '提示',
            content: '你已选择' + ids.length + '家机构，需在' + that.data.detail.valid_date + '天内前往学习，过期无效！',
            success(e) {
                if (e.confirm) {
                    wx.showLoading({
                        mask: true
                    })

                    app.request({
                        url: 'enrollment/selectSchool',
                        data: {
                            id: that.data.detail.id,
                            school_ids: ids.toString()
                        },

                        sCallback: (res) => {
                            if(res.code == 1) {
                                wx.redirectTo({
                                    url: '/pages/activity/select-success/index?id=' + that.data.id
                                })
                            } else {
                                wx.hideLoading();
                                app.alert(res.msg);
                            }
                        }
                    })
                }
            }
        })

    },
    select(e) {
      let index = e.detail.index;
      let detail = this.data.detail;
        let item = detail.school[index];
        let schoolId = item.schoolId;
        let ids = this.data.schoolIds;
        let num = detail.number_school;
            if (item.checked) {
                //取消选中
                let i = ids.indexOf(schoolId);
                ids.splice(i, 1);
            } else {
                //选中
                if (ids.length >= num) {
                    app.alert('最多选择' + num + '个机构');
                    return false;
                }
                ids.push(schoolId);
            }

            item.checked = !item.checked;
        detail.school[index] = item;
        this.setData({
          detail: detail,
          schoolIds: ids
        })

    }
})